﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Easydp.Models;
using Easydp.Models.Entities;
using System.ComponentModel;
using System.Data;
namespace Easydp.Controllers
{
    [DataObject()]
    public class OrderController
    {

        OrderManager ordermanager = new OrderManager();

        #region Get(Select) Method Region

        [DataObjectMethod(DataObjectMethodType.Select)]
        public TT_Orders GetAllOrders(int AgentID)
        {
            return ordermanager.GetAllOrders(AgentID);
        }


        [DataObjectMethod(DataObjectMethodType.Select)]
        /// <summary>
        /// 得到模型
        /// </summary>
        public List<TT_Orders> GetModel(int AgentID, string ordernum, string inDate, string outDate)
        {
            return ordermanager.GetModel(AgentID, ordernum, inDate, outDate);
        }
        public TT_Orders getmodel(string sqlWhere)
        {
            return ordermanager.getmodel(sqlWhere);
        }

        public TT_Orders GetModel(string sqlWhere)
        {
            return ordermanager.getmodel(sqlWhere);
        }

        [DataObjectMethod(DataObjectMethodType.Select)]
        public List<TT_Orders> GetListOrders(int AgentID)
        {
            return ordermanager.GetListOrders(AgentID);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        /// 
        public DataSet GetDataset(string Where)
        {
            return ordermanager.GetDataset(Where);
        }
        /// <summary>
        /// 订单列表 状态为>2
        /// </summary>
        /// <param name="AgentID"></param>
        /// <param name="ordernum"></param>
        /// <param name="inDate"></param>
        /// <param name="outDate"></param>
        /// <returns></returns>
        public DataSet GetDataSet(int AgentID, string ordernum, string inDate, string outDate)
        {
            string SqlWhere = "  OrderState>2 AND AgentID=" + AgentID + " ";
            if (!string.IsNullOrEmpty(ordernum))
            {
                SqlWhere += " and OrderNum = '" + ordernum + "' ";
            }
            if (inDate != "yyyy-mm-dd" && !string.IsNullOrEmpty(inDate))
            {
                SqlWhere += " and CreateOn >='" + inDate + "'";
            }
            if (outDate != "yyyy-mm-dd" && !string.IsNullOrEmpty(outDate))
            {
                SqlWhere += " and CreateOn <= '" + outDate + "'";
            }
            if ((inDate == "yyyy-mm-dd" || inDate == "" || inDate == null) && (outDate == "yyyy-mm-dd" || outDate == "" || outDate == null))
            {
                SqlWhere += " and CreateOn >= (getdate()-3)";
            }
            SqlWhere += " order by CreateOn";


            return ordermanager.GetDataset(SqlWhere);
        }

        public DataSet GetNewOrders(int AgentID, string ordernum, string inDate, string outDate)
        {
            string SqlWhere = "  AgentID=" + AgentID + " and  OrderState = 2 ";
            if (ordernum != "" && ordernum != null)
            {
                SqlWhere += " and OrderNum = '" + ordernum + "' ";
            }
            if (inDate != "yyyy-mm-dd" && inDate != "" && inDate != null)
            {
                SqlWhere += " and CreateOn >='" + inDate + "'";
            }
            if (outDate != "yyyy-mm-dd" && outDate != "" && outDate != null)
            {
                SqlWhere += " and CreateOn <= '" + outDate + "'";
            }
            SqlWhere += " order by CreateOn";

            return ordermanager.GetDataset(SqlWhere);
        }

        public List<TT_Orders> GetList(int AgentID, string ordernum, string inDate, string outDate)
        {
            return ordermanager.GetList(AgentID, ordernum, inDate, outDate);
        }

        #endregion 

        #region Update Method Region

        [DataObjectMethod(DataObjectMethodType.Update)]
        public void UpdateOrders(TT_Orders model)
        {
            try
            {
                int i = ordermanager.Update(model);
                if (i <= 0)
                    throw new Exception("更新失败");
            }
           
             catch (Exception ex)
           {
               throw new Exception(ex.Message);
           }
            
        }
        [DataObjectMethod(DataObjectMethodType.Update)]
        public string UpdateOrders(int OrderID,int State)
        {
            try
            {
                int i = ordermanager.Update(OrderID,State);
                return i.ToString();
                    
            }

            catch (Exception ex)
            {
                return ex.ToString();
            }

        }

        #endregion 

        #region Insert Method Region

        public int InsertOrder(TT_Orders obj)
        {
            try
            {
                if (obj.UserID <= 0)
                    obj.UserID = 0;
               return ordermanager.Insert(obj);
            }
            catch(Exception e)
            {
                return -1; ;
                throw new Exception(e.Message);
            }
        }

        #endregion

        #region Pay Region

        public TT_Orders GetWaitPayOrder(string orderID)
        {
            try
            {
                return ordermanager.GetWaitPayOrder(orderID);
            }
            catch(Exception e)
            {
                return null;
                throw new Exception(e.Message);
            }
          
        }

        public static string GetStateDispay(int state)
        {
            switch (state)
            {
                case 1:
                    return "未支付新订单";

                case 2:
                    return "已支付等待出票";

                case 3:
                    return "出票完成";

                case 4:
                    return "退票";
                case 18:
                    return "交易完成";
                default:
                    return "未知状态:" + state.ToString();
            }
            //return "";
        }

        #endregion
    }
}
